const app = getApp()
import { index, apply } from '../../../../api/mine/cash'

Page({
  data: {
    theme: '',
    back: true,
    title: '申请提现',
    loading: true,
    submitLoading: false,
    bonus: 0, // 可提
    setting: {},
    amount: '',
    disabled: true
  },
  onLoad (options) {
    this.setData({
      loading: true,
      theme: app.global.theme
    })
    index().then(res => {
      this.setData({
        bonus: res.data.bonus,
        setting: res.data.setting
      })
    })
      .finally(() => {
        this.setData({ loading: false })
      })
  },
  onPullDownRefresh () { /* 监听用户下拉动作*/ },
  onReachBottom () { /* 页面上拉触底事件的处理函数*/ },

  // ===================method================//

  apply (event) {
    if (this.data.bonus < this.data.setting.cash_limit) {
      return false
    }
    if (this.data.amount > this.data.bonus) {
      return false
    }
    if (!this.data.amount) {
      return false
    }
    this.setData({ submitLoading: true })
    apply(this.data.amount).then(res => {
      app.toast('申请已提交', function () {
        wx.navigateBack({
          delta: 1
        })
      }, 'success')
    })
      .finally(() => {
        this.setData({ submitLoading: false })
      })
  },

  setAmount (event) {
    if (event.detail > this.data.bonus) {
      this.setData({ amount: this.data.bonus })
    } else {
      this.setData({ amount: event.detail })
    }

    if (this.data.bonus >= this.data.setting.cash_limit && this.data.amount >= this.data.setting.cash_limit) {
      this.setData({ disabled: false })
    } else {
      this.setData({ disabled: true })
    }
  },

  all () {
    this.setData({ amount: this.data.bonus })

    if (this.data.bonus >= this.data.setting.cash_limit && this.data.amount >= this.data.setting.cash_limit) {
      this.setData({ disabled: false })
    } else {
      this.setData({ disabled: true })
    }
  }
})